package lgz

import (
	"bytes"
	"encoding/json"
	"errors"
	"fmt"
	"io/ioutil"
	"net/http"
)

func httpGet(url string) (map[string]interface{}, error) {
	m := make(map[string]interface{})
	resp, err := http.Get(url)
	defer resp.Body.Close()
	if err != nil {
		return m, err
	}
	if resp.StatusCode != http.StatusOK {
		return m, errors.New("Error:status code" + fmt.Sprintf("%v", resp.StatusCode))
	}
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return m, err
	}
	json.Unmarshal(body, &m)
	return m, nil
}

func httpPost(url string, payload map[string]interface{}) (map[string]interface{}, error) {
	m := make(map[string]interface{})
	bytesData, err := json.Marshal(payload)
	if err != nil {
		return m, err
	}
	reader := bytes.NewReader(bytesData)
	request, err := http.NewRequest("POST", url, reader)
	if err != nil {
		return m, err
	}
	request.Header.Set("Content-Type", "application/json;charset=UTF-8")
	client := http.Client{}
	resp, err := client.Do(request)
	if err != nil {
		return m, err
	}
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return m, err
	}
	json.Unmarshal(body, &m)
	return m, nil
}
func qsStringify(params map[string]string) (str string) {
	for k, v := range params {
		str = str + k + "=" + v + "&"
	}
	str = str[0 : len(str)-1]
	return str
}

func interfaceArrToStrArr(
	arr []interface{},
) (
	result []string,
) {
	for i := 0; i < len(arr); i++ {
		result = append(result, arr[i].(string))
	}
	return
}
